package com.zhongwang.business.module.system.index;

import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.Role;
import com.zhongwang.business.module.system.role.RoleService;
import com.zhongwang.business.module.system.user.UserService;
import com.zhongwang.business.module.taskManage.service.UnassignedTaskService;
import com.zhongwang.business.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 *  主页 控制器
 */
@Controller
public class IndexController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UnassignedTaskService unassignedTaskService;
    /**
     * 修改密码
     *
     * @param password 新密码
     * @return json
     */
    @ResponseBody
    @PostMapping("/update/user/pwd")
    @RequiresAuthentication
    public JsonResult updatePassword(String password) {
        return jsonResult(userService.updateUserPassword(password));
    }

    /**
     * 主页
     *
     * @return 页面路径
     */
    @GetMapping("/index")
    public String index(Model model) {
        //获取角色码用来进行控制页面显示内容
        List<Role> roles = roleService.findRolesByUserId(ShiroUtils.getUser().getId());
        List<String> roleCodes = new ArrayList<>();
        for (Role role:roles) {
            if(role.getRoleCode().equals("W01")){
                roleCodes.add(role.getRoleCode());
            }else if(role.getRoleCode().equals("W02")){
                roleCodes.add(role.getRoleCode());
            }else if(role.getRoleCode().equals("W03")){
                roleCodes.add(role.getRoleCode());
            }
        }
        if(ShiroUtils.getUser().getAccount().equals("admin")){
            roleCodes.add("admin");
        }
        model.addAttribute("roleCodes",roleCodes);
        model.addAttribute("unTaskCnt",unassignedTaskService.unTaskCnt());
        return "index";
    }

    /**
     * 获取用户未读互动消息
     *
     * @return json
     */
    /*@ResponseBody
    @GetMapping("/receiver")
    @RequiresAuthentication
    public List<MessageModel> receiver() {
        return userService.findMessageByType(MESSAGE_TYPE_INTERACTIVE);
    }*/

    /**
     * 获取用户未读任务下达消息
     *
     * @return json
     */
    /*@ResponseBody
    @GetMapping("/send")
    @RequiresAuthentication*/
    /*public List<MessageModel> send() {
        return userService.findMessageByType(MESSAGE_TYPE_SEND);
    }*/

    /**
     * 获取用户未读项目变更消息
     *
     * @return json
     */
    /*@ResponseBody
    @GetMapping("/change")
    @RequiresAuthentication
    public List<MessageModel> change() {
        return userService.findMessageByType(MESSAGE_TYPE_CHANGE);
    }*/

}
